package com.handinfo.android.ui.window;

import java.io.DataInputStream;
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArrayList;

import android.graphics.Bitmap;
import android.widget.Toast;

import com.handinfo.android.DWGameManager;
import com.handinfo.android.core.net.GameProtocol;
import com.handinfo.android.game.GameMessage;
import com.handinfo.android.game.Role;
import com.handinfo.android.game.skill.RoleCombatSkill;
import com.handinfo.android.game.skill.SkillConstants;
import com.handinfo.android.ui.IUIWindows;
import com.handinfo.android.ui.UIWindows;
import com.handinfo.android.uicontrols.DWControlsManager;
import com.handinfo.android.uicontrols.DWDragListener;
import com.handinfo.android.uicontrols.DWGestureListener;
import com.handinfo.android.uicontrols.DWListener;
import com.handinfo.android.uicontrols.DWLongListener;
import com.handinfo.android.uicontrols.controls.DWBackground;
import com.handinfo.android.uicontrols.controls.DWButton;
import com.handinfo.android.uicontrols.controls.DWControl;
import com.handinfo.android.uicontrols.controls.DWFrame;
import com.handinfo.android.uicontrols.controls.DWGrid;
import com.handinfo.android.uicontrols.controls.DWImageBox;
import com.handinfo.android.uicontrols.controls.DWLabel;
import com.handinfo.android.uicontrols.controls.DWListSubItem;
import com.handinfo.android.uicontrols.controls.DWListbox;
import com.handinfo.android.uicontrols.controls.DWTabControl;
import com.handinfo.android.uicontrols.controls.DWTabPage;
import com.handinfo.android.uicontrols.controls.DWTextbox;
import com.handinfo.android.uicontrols.controls.DWTitle;
import com.handinfo.android.utils.Tools;

/**
 * 
 * @author Xiaoyingying
 * 
 * 技能窗口
 * 
 */
public class UISetSkillWindow implements IUIWindows
{
	private Bitmap FENGEBACKGROUND;//分割条图片
	private Bitmap MESSAGEBACKGROUND;//提示信息背景图片
	private Bitmap BUTTONBACKGROUND;//按钮图片
	private Bitmap ANXIABACKGROUND;//按下按钮图片
	private Bitmap JINENGBACKGROUND;//技能方块背景图片
	private Bitmap CLOSEWINDOW;
	private Bitmap tabSelectImg;// 选项卡选中的图标
	private Bitmap tabUnSelectImg;// 先选卡未被选中的图标
	private Bitmap GRIDBACKGROUND;//格子背景
	private Bitmap titleImg;
	private Bitmap FRAMEBACKGROUND;//背景图片
	private Bitmap BUTTONIMG;
	private Bitmap ANXIAIMG;
	private Bitmap RESETBUTTON;
	private Bitmap RESETANXIA;

	private String[] m_skill_tabPage_name = { "技能", "绝技" }; //标签页标题

	private DWFrame m_skill_Frame; //技能界面
	private DWTitle m_skill_title; //技能界面标题
	private DWImageBox m_skill_close;
	private DWBackground m_skill_background1; //左边的背景
	private DWBackground m_skill_background2; //右边的背景
	private DWBackground m_skill_fgbackground; //分割图片
	private DWBackground m_skill_messagbox; //提示信息背景
	private DWLabel m_skill_jineng; //技能标签
	private DWLabel m_skill_jveji; //绝技标签
	private DWTextbox m_skill_xiuwei; //精魂标签
	private DWTextbox m_skill_message; //提示信息
	private DWGrid[] m_skill_jineng_grid = new DWGrid[4]; //角色使用的技能

	private DWBackground[][] m_skill_bag_img = new DWBackground[m_skill_tabPage_name.length][];

	private DWTabControl m_skill_tabcontrol; //标签页控件
	private DWTabPage[] m_skill_tabPage = new DWTabPage[m_skill_tabPage_name.length];
	private DWListbox[] m_skill_listbox = new DWListbox[m_skill_tabPage_name.length];
	private DWButton m_skill_reset;

	private String str = "<mark c=FFBB11>点击图标可以查看及学习，拖动到左侧当前技能可装备</mark>";
	private boolean m_window_isOpen = false;
	private RoleCombatSkill selctSkill = null;

	/**
	 * 更新窗口数据
	 */
	public void updateWindow()
	{
		if (m_window_isOpen)
		{
			initDate();
			setLetfColumn();
			setXiuwei();
			for (int i = 0; i < m_skill_skillList.length; i++)
			{
				if (m_skill_skillList[i] != null)
				{
					addListSubItem(i);
				}
			}
		}
	}

	private void initFrame()
	{
		if (DWControlsManager.getInstance().contains(m_skill_Frame))
		{
			DWControlsManager.getInstance().removeControl(m_skill_Frame);
			m_skill_Frame = null;
		}

		m_skill_Frame = new DWFrame(DWFrame.FRAME_TYPE_FULL);

		m_skill_title = new DWTitle("", m_skill_Frame);
		m_skill_title.setBackground(titleImg, false);
		m_skill_Frame.addControl(m_skill_title);
		m_skill_Frame.setClickClose(false);

		m_skill_close = new DWImageBox(CLOSEWINDOW);
		m_skill_close.setNearAnchor(m_skill_title, Tools.RIGHT_VCENTER, Tools.RIGHT_VCENTER);
		m_skill_close.setTouchZoomIn(UIFixWindow.TOUCHZOOM, UIFixWindow.TOUCHZOOM);
		m_skill_close.addListener(new DWListener() {

			public void OnDoubleClick()
			{
			}

			public void OnClick()
			{
				close((byte) 0);
			}
		});
		m_skill_Frame.addControl(m_skill_close);
		m_skill_background1 = new DWBackground((m_skill_Frame.getShowWidth() - 50) / 3,
				m_skill_Frame.getShowHeight() - 50 -tabSelectImg.getHeight()-BUTTONIMG.getHeight());
		m_skill_background1.setNearAnchor(m_skill_Frame, Tools.LEFT_TOP, Tools.LEFT_TOP, 20, tabSelectImg
				.getHeight() + 25);
		m_skill_Frame.addControl(m_skill_background1);

		m_skill_fgbackground = new DWBackground(FENGEBACKGROUND, m_skill_background1.getShowWidth(), FENGEBACKGROUND
				.getHeight());
		m_skill_fgbackground.setNearAnchor(m_skill_background1, Tools.LEFT_VCENTER, Tools.LEFT_VCENTER, 0,
				m_skill_background1.getShowHeight() / 8);
		m_skill_Frame.addControl(m_skill_fgbackground);

		m_skill_jineng = new DWLabel("技能");
		m_skill_jineng.setBackgroundColor(Tools.ALPHA);
		m_skill_jineng.setNearAnchor(m_skill_background1, Tools.LEFT_TOP, Tools.LEFT_TOP, 20, 20);
		m_skill_Frame.addControl(m_skill_jineng);

		m_skill_jveji = new DWLabel("绝技");
		m_skill_jveji.setBackgroundColor(Tools.ALPHA);
		m_skill_jveji.setNearAnchor(m_skill_fgbackground, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 10, 10);
		m_skill_Frame.addControl(m_skill_jveji);

		m_skill_background2 = new DWBackground((m_skill_Frame.getShowWidth() - 50) * 2 / 3, m_skill_background1.getShowHeight());
		m_skill_background2.setNearAnchor(m_skill_Frame, Tools.RIGHT_TOP, Tools.RIGHT_TOP, -20, tabSelectImg
				.getHeight() + 25);
		m_skill_Frame.addControl(m_skill_background2);

		m_skill_xiuwei = new DWTextbox("");
		m_skill_Frame.addControl(m_skill_xiuwei);

		m_skill_tabcontrol = new DWTabControl(tabUnSelectImg, tabSelectImg, m_skill_background2.getShowWidth() - 20,
				m_skill_background2.getShowHeight() * 3 / 4 + tabSelectImg.getHeight());
		m_skill_tabcontrol.setNearAnchor(m_skill_background2, Tools.HCENTER_TOP, Tools.HCENTER_TOP, 0, -tabSelectImg
				.getHeight());

		m_skill_tabcontrol.addListener(new DWListener() {

			public void OnDoubleClick()
			{
			}

			public void OnClick()
			{
				if (selctSkill != null)
				{
					putListenner();
					selctSkill = null;
				}
			}
		});

		for (int i = 0; i < m_skill_tabPage_name.length; i++)
		{
			Tools.debugPrintln("tablepag=" + i);
			m_skill_tabPage[i] = new DWTabPage();
			m_skill_listbox[i] = new DWListbox(m_skill_tabcontrol.getShowWidth() - 10,
					m_skill_tabcontrol.getShowHeight() - tabSelectImg.getHeight());
			m_skill_listbox[i].setLineSpacing(5);
			m_skill_listbox[i].setNearAnchor(m_skill_tabPage[i], Tools.LEFT_TOP, Tools.LEFT_TOP, 0, 10);
			m_skill_listbox[i].setShowBackgroundRect(false);
			m_skill_tabPage[i].addControls(m_skill_listbox[i]);
			m_skill_tabcontrol.addTabPage(m_skill_tabPage[i]);
			m_skill_tabPage[i].setName(m_skill_tabPage_name[i]);
			m_skill_listbox[i].addListener(new DWListener() {
				public void OnDoubleClick()
				{
				}

				public void OnClick()
				{
					if (selctSkill != null)
					{
						putListenner();
						selctSkill = null;
					}
				}
			});
		}
		m_skill_Frame.addControl(m_skill_tabcontrol);

		m_skill_messagbox = new DWBackground(MESSAGEBACKGROUND, m_skill_tabcontrol.getShowWidth(), m_skill_background2
				.getShowHeight() / 4);
		m_skill_messagbox.setNearAnchor(m_skill_tabcontrol, Tools.HCENTER_TOP, Tools.HCENTER_BOTTOM);
		m_skill_Frame.addControl(m_skill_messagbox);

		m_skill_message = new DWTextbox(str, m_skill_messagbox.getShowWidth() - 20);
		m_skill_message.setNearAnchor(m_skill_messagbox, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER);
		m_skill_Frame.addControl(m_skill_message);

		m_skill_reset = new DWButton("重置等级", BUTTONIMG);
		m_skill_reset.setDownImage(ANXIAIMG);
		m_skill_reset.setNearAnchor(m_skill_background2, Tools.RIGHT_TOP, Tools.RIGHT_BOTTOM, -20, 5);
		m_skill_reset.addListener(new DWListener() {
			public void OnDoubleClick()
			{
			}

			public void OnClick()
			{
//				showResetFam("您确定要重置所有技能等级？`需要达到vip1,花费300元宝");
				if(m_reset_viplv>-1&&m_reset_fee>-1)
				{
					showResetFam("您确定要重置所有技能等级？`需要达到vip"+m_reset_viplv+",花费"+m_reset_fee+"元宝");
				}else
				{
					DWGameManager.getInstance().getSendMessage().sendRestSkill(GameProtocol.CS_OPEN_RESET_SKILL);
				}			
			}
		});
		m_skill_Frame.addControl(m_skill_reset);

		creatLeftColumn();
	}

	/**
	 * 重置修为值
	 */
	public void setXiuwei()
	{
		if (m_skill_Frame == null || (!DWControlsManager.getInstance().contains(m_skill_Frame)))
			return;

		if (m_skill_xiuwei != null)
		{
			m_skill_xiuwei.setText("修为：<mark c=FF3300>" + DWGameManager.getInstance().m_role.m_xiuwei + "</mark>");
			m_skill_xiuwei.setNearAnchor(m_skill_background1, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 20, 5);
		}
	}
	
	

	/**
	 * 获取技能和装备的技能数据
	 */
	private RoleCombatSkill[][] m_skill_skillList = new RoleCombatSkill[2][];

	private void initDate()
	{
		if (DWGameManager.getInstance().m_role != null)
		{
			subRoleSkill(); //获取技能绝技数组
		}
	}

	private void subRoleSkill()
	{
		CopyOnWriteArrayList<RoleCombatSkill> m_roleCombatSkill = DWGameManager.getInstance().m_role.m_roleCombatSkill;

		for (int i = 0; i < 2; i++)
		{
			CopyOnWriteArrayList<RoleCombatSkill> array = new CopyOnWriteArrayList<RoleCombatSkill>();
			for (RoleCombatSkill role : m_roleCombatSkill) {
				Tools.debugPrintln("m.name=" + role.m_name);
				if (role != null)
				{
					switch (i) {
						case 0:
							if (role.m_type != 3)
								array.add(role);
							break;

						case 1:
							if (role.m_type == 3)
								array.add(role);
							break;
					}
				}
			}
			int size = array.size();
			m_skill_skillList[i] = new RoleCombatSkill[size];
			for (int j = 0; j < size; j++) {
				RoleCombatSkill m = array.get(j);
				m_skill_skillList[i][j] = m;
				Tools.debugPrintln("m.name=" + m.m_name);
			}
			array = null;
		}
	}

	/**
	 * 绘制左边的装备技能
	 */
	private void creatLeftColumn()
	{
		int moveX = (m_skill_background1.getShowWidth() - GRIDBACKGROUND.getWidth() * 3) / 7;
		int moveY = ((m_skill_background1.getShowHeight() * 5) / 8 - GRIDBACKGROUND.getHeight() - 20) / 3;
		for (int i = 0; i < 3; i++)
		{
			final int index = i;
			m_skill_jineng_grid[i] = new DWGrid(GRIDBACKGROUND);
			m_skill_jineng_grid[i].setShowWideHigh(GRIDBACKGROUND.getWidth() + 10, GRIDBACKGROUND.getHeight() + 10);
			m_skill_jineng_grid[i].setDarg(true);
			m_skill_jineng_grid[i].setSelectType(DWGrid.SELECT_TYPE_ARC2);
			if (i == 2)
				m_skill_jineng_grid[i].setNearAnchor(m_skill_fgbackground, Tools.LEFT_BOTTOM, Tools.LEFT_TOP, moveX,
						-10);
			else if (i == 1)
				m_skill_jineng_grid[i].setNearAnchor(m_skill_fgbackground, Tools.LEFT_BOTTOM, Tools.LEFT_TOP,
						moveX * 3 + GRIDBACKGROUND.getWidth(), -(10 + moveY * 2));
			else if (i == 0)
				m_skill_jineng_grid[i].setNearAnchor(m_skill_fgbackground, Tools.LEFT_BOTTOM, Tools.LEFT_TOP,
						(moveX * 6) + (GRIDBACKGROUND.getWidth() * 2), -(10 + moveY * 3));
			m_skill_Frame.addControl(m_skill_jineng_grid[i]);

			m_skill_jineng_grid[i].addDragListener(new DWDragListener() {

				public void OnDrag(DWControl control)
				{
					if (control instanceof DWGrid)
					{
						DWGrid grid = (DWGrid) control;
						if (((RoleCombatSkill) grid.m_data).m_type != 3)
						{
							m_skill_jineng_grid[index].m_data = grid.m_data;
							DWGameManager.getInstance().m_role.setShortcut(((RoleCombatSkill) grid.m_data).m_id, index);
						}
					}
				}
			});
			m_skill_jineng_grid[i].addListener(new DWListener() {

				public void OnDoubleClick()
				{
				}

				public void OnClick()
				{
					if (selctSkill != null)
					{
						putListenner();
						if (selctSkill.m_type != SkillConstants.绝技)
						{
							m_skill_jineng_grid[index].addObject(selctSkill);
							DWGameManager.getInstance().m_role.setShortcut(selctSkill.m_id, index);
						}
						selctSkill = null;
					}
				}
			});
		}

		m_skill_jineng_grid[3] = new DWGrid(GRIDBACKGROUND);
		m_skill_jineng_grid[3].setShowWideHigh(GRIDBACKGROUND.getWidth() + 10, GRIDBACKGROUND.getHeight() + 10);
		m_skill_jineng_grid[3].setDarg(true);
		m_skill_jineng_grid[3].setNearAnchor(m_skill_fgbackground, Tools.HCENTER_TOP, Tools.HCENTER_BOTTOM, 0, 30);
		m_skill_jineng_grid[3].addDragListener(new DWDragListener() {
			public void OnDrag(DWControl control)
			{
				if (control instanceof DWGrid)
				{
					DWGrid grid = (DWGrid) control;
					if (((RoleCombatSkill) grid.m_data).m_type == SkillConstants.绝技)
					{
						m_skill_jineng_grid[3].m_data = grid.m_data;
						DWGameManager.getInstance().m_role.setShortcut(((RoleCombatSkill) grid.m_data).m_id, 3);
					}
				}
			}
		});
		m_skill_jineng_grid[3].addListener(new DWListener() {
			public void OnDoubleClick()
			{
			}

			public void OnClick()
			{
				if (selctSkill != null)
				{
					putListenner();
					if (selctSkill.m_type == SkillConstants.绝技)
					{
						m_skill_jineng_grid[3].addObject(selctSkill);
						DWGameManager.getInstance().m_role.setShortcut(selctSkill.m_id, 3);
					}
					selctSkill = null;
				}
			}
		});
		m_skill_jineng_grid[3].setSelectType(DWGrid.SELECT_TYPE_ARC2);
		m_skill_Frame.addControl(m_skill_jineng_grid[3]);
	}

	/**
	 * 设置装备技能
	 */
	private void setLetfColumn()
	{
		for (int i = 0; i < m_skill_jineng_grid.length; i++)
		{
			m_skill_jineng_grid[i].m_data = null;
			m_skill_jineng_grid[i].addObject(DWGameManager.getInstance().m_role.getShortcutObject(i));
		}
	}

	private DWGrid select_grid = null;
	private DWGrid[][] m_skill_bag_grid = new DWGrid[2][];
	private DWButton[][] m_skill_bag_button = new DWButton[2][];

	/**
	 * 绘制右边技能和绝技标签页内的内容
	 * @param num
	 */
	private void addListSubItem(final int num)
	{
		m_skill_listbox[num].removeAllSubItem();

		m_skill_bag_img[num] = new DWBackground[m_skill_skillList[num].length];
		DWListSubItem[] item = new DWListSubItem[(m_skill_skillList[num].length + 1) / 2];
		m_skill_bag_grid[num] = new DWGrid[m_skill_skillList[num].length];
		m_skill_bag_button[num] = new DWButton[m_skill_skillList[num].length];

		for (int i = 0; i < m_skill_skillList[num].length; i++) {
			final int index = i;
			m_skill_bag_img[num][i] = new DWBackground(JINENGBACKGROUND,
					(m_skill_listbox[num].getShowWidth() - 15) / 2, (m_skill_listbox[num].getShowHeight() - 20) / 2);

			if (i % 2 == 0) {
				item[i / 2] = new DWListSubItem();
				m_skill_bag_img[num][i].setNearAnchor(item[i / 2], Tools.LEFT_TOP, Tools.LEFT_TOP);
			}
			else {
				m_skill_bag_img[num][i].setNearAnchor(m_skill_bag_img[num][i - 1], Tools.LEFT_VCENTER,
						Tools.RIGHT_VCENTER, 5, 0);
			}

			item[i / 2].addControls(m_skill_bag_img[num][i]);

			m_skill_bag_grid[num][i] = new DWGrid(GRIDBACKGROUND);
			m_skill_bag_grid[num][i].setSelectType(DWGrid.SELECT_TYPE_ARC1);
			m_skill_bag_grid[num][i].addLongListener(new DWLongListener() {

				public void OnLong(boolean isDown)
				{
					if (isDown)
					{
						setGridSelect(m_skill_bag_grid[num][index]);
						touchListener(num);
					}
					else
						putListenner();
				}
			});
			m_skill_bag_grid[num][i].addListener(new DWListener() {
				public void OnDoubleClick()
				{
				}

				public void OnClick()
				{
					setGridSelect(m_skill_bag_grid[num][index]);
					showCombatSkill(m_skill_skillList[num][index]);
				}
			});
			m_skill_bag_grid[num][i].addObject(m_skill_skillList[num][i]);

			if (m_skill_skillList[num][i].m_type == SkillConstants.被动)
				m_skill_bag_grid[num][i].setDarg(true);

			m_skill_bag_grid[num][i].setNearAnchor(m_skill_bag_img[num][i], Tools.LEFT_VCENTER, Tools.LEFT_VCENTER, 10,
					0);
			item[i / 2].addControls(m_skill_bag_grid[num][i]);

			DWLabel m_skill_bag_name = new DWLabel(m_skill_skillList[num][i].m_name);
			m_skill_bag_name.setBackgroundColor(Tools.ALPHA);
			m_skill_bag_name.setNearAnchor(m_skill_bag_img[num][i], Tools.LEFT_TOP, Tools.HCENTER_TOP, -20, 10);
			item[i / 2].addControls(m_skill_bag_name);

			if (m_skill_skillList[num][i].m_level < m_skill_skillList[num][i].m_levelMax)
			{
				m_skill_bag_button[num][i] = new DWButton("升级", BUTTONBACKGROUND);
				m_skill_bag_button[num][i].setDownImage(ANXIABACKGROUND);
				m_skill_bag_button[num][i].addListener(new DWListener() {
					public void OnDoubleClick()
					{
					}

					public void OnClick()
					{
						if (DWGameManager.getInstance().getSendMessage().isNetLocked(GameMessage.锁_技能))
							return;

						learnCombatSkill(m_skill_skillList[num][index]);
					}
				});
				m_skill_bag_button[num][i].setNearAnchor(m_skill_bag_img[num][i], Tools.LEFT_BOTTOM,
						Tools.HCENTER_BOTTOM,
						0, -5);
				item[i / 2].addControls(m_skill_bag_button[num][i]);
			}

			if (i % 2 == 0)
			{
				item[i / 2].refreshControl();
				m_skill_listbox[num].addSubItem(item[i / 2]);
			}
		}

	}

	/**
	 * 技能点亮
	 * @param grid
	 */
	private void setGridSelect(DWGrid grid)
	{
		if (select_grid != null)
		{
			select_grid.setSelect(false);
		}
		select_grid = grid;
		select_grid.setSelect(true);
	}

	/**
	 * 选中后装备栏点亮
	 * @param num
	 */
	private void touchListener(int num)
	{
		if (selctSkill != null)
		{
			putListenner();
			selctSkill = null;
		}
		switch (num) {
			case 0:
				for (int i = 0; i < 3; i++)
				{
					m_skill_jineng_grid[i].setSelect(true);

				}
				break;
			case 1:
				m_skill_jineng_grid[3].setSelect(true);
				break;
		}
	}

	/**
	 * 选中状态消失
	 */
	private void putListenner()
	{
		for (int i = 0; i < 4; i++)
		{
			m_skill_jineng_grid[i].setSelect(false);
		}
	}

	public void init()
	{
		FENGEBACKGROUND = UIWindows.createImage("/img/newui/jinengfgt_1.gnp");//分割条图片
		MESSAGEBACKGROUND = UIWindows.createImage("/img/newui/jinengtf_1.gnp");//提示信息背景图片
		BUTTONBACKGROUND = UIWindows.createImage("/img/newui/anniu_2.gnp");//按钮图片
		ANXIABACKGROUND = UIWindows.createImage("/img/newui/anniu_2ax.gnp");
		JINENGBACKGROUND = UIWindows.createImage("/img/newui/beibaobj_1.gnp");//技能方块背景图片
		CLOSEWINDOW = UIWindows.createImage("/img/newui/fanhuianniu_1.gnp");
		tabSelectImg = UIWindows.createImage("/img/newui/biaoqianye_2.gnp");
		tabUnSelectImg = UIWindows.createImage("/img/newui/biaoqianye_1.gnp");
		GRIDBACKGROUND = UIWindows.createImage("/img/newui/zhujiemiancy_1.gnp");
		titleImg = UIWindows.createImage("/img/newui/dangqianjineng_1.gnp");
		FRAMEBACKGROUND = UIWindows.createImage("/img/newui/beibaobj_2.gnp");
		BUTTONIMG = UIWindows.createImage("/img/newui/anniu_1.gnp");
		ANXIAIMG = UIWindows.createImage("/img/newui/anniu_1ax.gnp");
		RESETBUTTON = UIWindows.createImage("/img/newui/anniu_6.gnp");
		RESETANXIA = UIWindows.createImage("/img/newui/anniu_6ax.gnp");
	}

	DWFrame m_learn_skill;
	DWButton learn;

	private void learnCombatSkill(final RoleCombatSkill skill)
	{
		if (skill.m_next_lvl_skill == null)
			return;

		if (DWControlsManager.getInstance().contains(m_learn_skill))
			DWControlsManager.getInstance().removeControl(m_learn_skill);

		DWGameManager.getInstance();
		m_learn_skill = new DWFrame(DWGameManager.Screen_Width / 2 - 20,
				DWGameManager.getInstance().Screen_Height * 3 / 5);
		m_learn_skill.setBackgroundColor(Tools.ALPHA);
		m_learn_skill.setTouchZoomIn(0, 0);

		DWBackground m_learn_background = new DWBackground(FRAMEBACKGROUND, m_learn_skill.getShowWidth(), m_learn_skill
				.getShowHeight());
		m_learn_background.setNearAnchor(m_learn_skill, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER);
		m_learn_skill.addControl(m_learn_background);

		String learnNextLv = "<mark c=FFBB11>升级所需角色等级：" + skill.m_next_lvl_skill.m_learn_lv + "</mark>";
		DWTextbox learnLv = new DWTextbox(learnNextLv);
		learnLv.setNearAnchor(m_learn_skill, Tools.LEFT_BOTTOM, Tools.LEFT_BOTTOM, 40, -10);
		m_learn_skill.addControl(learnLv);

		long xiuwei_num = DWGameManager.getInstance().m_role.m_xiuwei;
		String xiuweiNum = "升级所需修为：<mark c=" + (xiuwei_num > skill.m_need_sp ? "55FF00" : "ff3300") + ">"
				+ skill.m_need_sp + "</mark>";
		DWTextbox xiuwei = new DWTextbox(xiuweiNum);
		xiuwei.setNearAnchor(learnLv, Tools.LEFT_BOTTOM, Tools.LEFT_TOP, 0, -5);
		m_learn_skill.addControl(xiuwei);

		DWListbox listbox = new DWListbox(m_learn_skill.getShowWidth() - 50, m_learn_skill.getShowHeight()
				- learnLv.getShowHeight() * 2 - 40);
		listbox.setShowBackgroundRect(false);
		listbox.setNearAnchor(m_learn_skill, Tools.LEFT_TOP, Tools.LEFT_TOP, 20, 20);

		DWListSubItem sunitem = new DWListSubItem();

		DWGrid grid = new DWGrid(GRIDBACKGROUND);
		grid.setDarg(true);
		grid.addObject(skill);
		grid.setNearAnchor(sunitem, Tools.LEFT_TOP, Tools.LEFT_TOP, 20, 20);
		sunitem.addControls(grid);

		DWTextbox skill_name = new DWTextbox(skill.m_name);
		skill_name.setNearAnchorUn(grid, Tools.LEFT_VCENTER, Tools.RIGHT_VCENTER, 20, 0);
		sunitem.addControls(skill_name);

		String str1 = "<mark c=FFBB11>当前级别：</mark>" + skill.m_level + "`<mark c=FFBB11>射程：</mark>" + skill.m_range
				+ "`" + skill.m_description + "`  ";
		Tools.debugPrintln(str1);
		DWTextbox messge = new DWTextbox(str1, (listbox.getShowWidth() - 60) / 2);
		messge.setNearAnchor(grid, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 0, 10);
		sunitem.addControls(messge);

		String str2 = "<mark c=FFBB11>下一级别：</mark>" + (skill.m_next_lvl_skill.m_level) + "`<mark c=FFBB11>射程：</mark>"
				+ skill.m_next_lvl_skill.m_range + "`" + skill.m_next_lvl_skill.m_description + "`  ";
		DWTextbox messge2 = new DWTextbox(str2, messge.getShowWidth());
		messge2.setNearAnchor(messge, Tools.LEFT_VCENTER, Tools.RIGHT_VCENTER, 10, 0);
		sunitem.addControls(messge2);

		listbox.addSubItem(sunitem);
		m_learn_skill.addControl(listbox);

		if (skill.m_level != skill.m_levelMax)
		{
			learn = new DWButton("升级", BUTTONBACKGROUND);
			learn.setDownImage(ANXIABACKGROUND);
			learn.setNearAnchor(m_learn_skill, Tools.RIGHT_BOTTOM, Tools.RIGHT_BOTTOM, -40, -20);
			learn.addListener(new DWListener() {
				public void OnDoubleClick()
				{
				}

				public void OnClick()
				{
					DWControlsManager.getInstance().removeControl(m_learn_skill);

					if (DWGameManager.getInstance().m_role.m_xiuwei < skill.m_need_sp)
					{
						DWGameManager.getInstance().showToast("修为不足~", Toast.LENGTH_SHORT);
					}
					else if (DWGameManager.getInstance().m_role.m_lv < skill.m_next_lvl_skill.m_learn_lv)
					{
						DWGameManager.getInstance().showToast("等级不足~", Toast.LENGTH_SHORT);
					}
					else
					{
						DWGameManager.getInstance().m_role.learnCombatSkill(skill);
					}
				}
			});
			m_learn_skill.addControl(learn);
		}

		DWControlsManager.getInstance().addControl(m_learn_skill);
	}

	/**
	 * 显示技能信息
	 * @param skill
	 */
	private DWButton m_skill_setSkill;

	private void showCombatSkill(final RoleCombatSkill skill)
	{
		if (DWControlsManager.getInstance().contains(m_learn_skill))
			DWControlsManager.getInstance().removeControl(m_learn_skill);

		m_learn_skill = new DWFrame(DWGameManager.Screen_Width / 3, DWGameManager.Screen_Height / 2 + 40);
		m_learn_skill.setBackgroundColor(Tools.ALPHA);
		m_learn_skill.setTouchZoomIn(0, 0);

		DWBackground m_learn_background = new DWBackground(FRAMEBACKGROUND, m_learn_skill.getShowWidth(), m_learn_skill
				.getShowHeight());
		m_learn_background.setNearAnchor(m_learn_skill, Tools.HCENTER_VCENTER, Tools.HCENTER_VCENTER);
		m_learn_skill.addControl(m_learn_background);

		DWListbox m_skill_list = new DWListbox(m_learn_skill.getShowWidth() - 20, m_learn_skill.getShowHeight() - (skill.m_type == SkillConstants.被动?0:BUTTONBACKGROUND.getHeight())-30);
		m_skill_list.setNearAnchor(m_learn_skill, Tools.HCENTER_TOP, Tools.HCENTER_TOP, 0,15);
		m_skill_list.setShowBackgroundRect(false);

		DWListSubItem m_skill_subitem = new DWListSubItem();

		DWGrid grid = new DWGrid(GRIDBACKGROUND);
		grid.setDarg(true);
		grid.addObject(skill);
		grid.setNearAnchor(m_skill_subitem, Tools.LEFT_TOP, Tools.LEFT_TOP);
		m_skill_subitem.addControls(grid);

		String title = skill.m_name + "`";
		DWTextbox skill_name = new DWTextbox(title);
		skill_name.setNearAnchorUn(grid, Tools.LEFT_VCENTER, Tools.RIGHT_VCENTER, 20, 0);
		m_skill_subitem.addControls(skill_name);

		String str = "";

		if (skill.m_type != 1)
		{
			str += "<mark c=FFBB11>消耗：</mark>" + skill.m_consumption + "`<mark c=FFBB11>冷却时间：</mark>" + skill.m_cd
					+ "s`<mark c=FFBB11>施法时间：</mark>"
					+ (skill.m_type == 0 ? "瞬发" : skill.m_castingTime + "s") + "`<mark c=FFBB11>射程：</mark>"
					+ skill.m_range + "`";
		}

		if (skill.m_level < skill.m_levelMax)
			str += "<mark c=FFBB11>升下一级所需修为：</mark>" + skill.m_need_sp + "`";

		str += skill.m_description;
		Tools.debugPrintln(str);
		DWTextbox messge = new DWTextbox(str, m_skill_list.getShowWidth());
		messge.setNearAnchor(grid, Tools.LEFT_TOP, Tools.LEFT_BOTTOM, 0, 10);
		m_skill_subitem.addControls(messge);

		m_skill_list.addSubItem(m_skill_subitem);
		m_learn_skill.addControl(m_skill_list);

		if (skill.m_type != SkillConstants.被动)
		{
			m_skill_setSkill = new DWButton("设置", BUTTONBACKGROUND);
			m_skill_setSkill.setNearAnchor(m_skill_list, Tools.HCENTER_TOP, Tools.HCENTER_BOTTOM, 0, 5);
			m_skill_setSkill.addListener(new DWListener() {
				public void OnDoubleClick()
				{
				}

				public void OnClick()
				{
					DWControlsManager.getInstance().removeControl(m_learn_skill);
					int type = skill.m_type == SkillConstants.绝技 ? 1 : 0;
					touchListener(type);
					selctSkill = skill;
				}
			});
			m_learn_skill.addControl(m_skill_setSkill);
		}
		DWControlsManager.getInstance().addControl(m_learn_skill);
	}

	/**
	 * 技能洗点界面
	 */
	private DWFrame m_resetFrame;
	private DWBackground m_background;

	private void showResetFam(String message)
	{
		if (m_resetFrame == null)
		{
			m_resetFrame = new DWFrame(DWGameManager.Screen_Width * 2 / 5, DWGameManager.Screen_Width * 3 / 10);
			m_resetFrame.setClickClose(false);

			DWTitle m_Title = new DWTitle("提示", m_resetFrame);
			m_resetFrame.addControl(m_Title);

			DWImageBox m_close = new DWImageBox(CLOSEWINDOW);
			m_close.setNearAnchor(m_Title, Tools.RIGHT_VCENTER, Tools.RIGHT_VCENTER);
			m_close.setTouchZoomIn(UIFixWindow.TOUCHZOOM, UIFixWindow.TOUCHZOOM);
			m_close.setTop(true);
			m_close.addListener(new DWListener() {

				public void OnDoubleClick()
				{
				}

				public void OnClick()
				{
					DWControlsManager.getInstance().removeControl(m_resetFrame);		
				}
			});
			m_resetFrame.addControl(m_close);

			m_background = new DWBackground(m_resetFrame.getShowWidth() - 20, m_resetFrame.getShowHeight() - 40);
			m_background.setFill(true);
			m_background.setNearAnchor(m_resetFrame, Tools.HCENTER_BOTTOM, Tools.HCENTER_BOTTOM,0,-10);
			m_resetFrame.addControl(m_background);

			setResetMessage(message);

			DWButton m_button = new DWButton("确定", RESETBUTTON);
			m_button.setDownImage(RESETANXIA);
			m_button.addListener(new DWListener() {

				public void OnDoubleClick()
				{
				}

				public void OnClick()
				{
					Role m_role=DWGameManager.getInstance().m_role;
					if(m_role.m_rmb<m_reset_fee)
					{
						DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "重置失败，元宝不足");
					}else if(m_role.m_vip_lv<m_reset_viplv)
					{
						DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, "重置失败，VIP等级不够");
					}else{
						DWControlsManager.getInstance().removeControl(m_resetFrame);
						DWGameManager.getInstance().getSendMessage().sendRestSkill(GameProtocol.CS_RESET_SKILL_APPLICATION);
					}				
				}
			});
			m_button.setNearAnchor(m_background, Tools.LEFT_BOTTOM, Tools.LEFT_BOTTOM, 10, -30);
			m_resetFrame.addControl(m_button);
		}
		else
		{
			setResetMessage(message);		
		}
		
		if (!DWControlsManager.getInstance().contains(m_resetFrame))
			DWControlsManager.getInstance().addControl(m_resetFrame);
	}

	/**
	 * 设置技能洗点界面的提示信息
	 */
	private DWLabel m_message;

	private void setResetMessage(String message)
	{
		if (m_message != null)
		{
			m_resetFrame.removeControl(m_message);
			m_message = null;
		}
		m_message = new DWLabel(message);
		m_message.setShowWideHigh(m_background.getShowWidth() - 40, m_background.getShowHeight()
				- RESETBUTTON.getHeight() - 30);
		m_message.setBackgroundColor(Tools.ALPHA);
		m_message.setNearAnchor(m_background, Tools.HCENTER_TOP, Tools.HCENTER_TOP);
		m_resetFrame.addControl(m_message);

//		if(m_message==null)
//		{
//			m_message=new DWTextbox("");
//			m_resetFrame.addControl(m_message);
//		}
//		
//		int heigth=m_background.getShowHeight()-RESETBUTTON.getHeight()-30;		
//		m_message.setText(message);
//		if(m_message.getShowWidth()>(m_background.getShowWidth()-40))
//		{
//			m_message.setShowWideHigh(m_background.getShowWidth()-40, h)
//		}
//			
//		m_message.setNearAnchor(m_background, Tools.HCENTER_TOP, Tools.HCENTER_TOP, 0, heigth>0?(heigth-m_message.getShowHeight())/2:0);

	}

	public void open(byte index)
	{
		m_window_isOpen = true;
		initFrame();		
		DWControlsManager.getInstance().addControl(m_skill_Frame);
		updateWindow();
	}

	public void close(byte index)
	{
		selctSkill = null;
		m_window_isOpen = false;
		DWControlsManager.getInstance().removeControl(m_skill_Frame);
	}

	public void dispose()
	{
	}

	public void logic()
	{
	}

	/**
	 * 获取第一个技能
	 * @return
	 */
	public DWGrid getSkillGrid()
	{
		DWGrid grid = null;
		if (m_skill_Frame != null && DWControlsManager.getInstance().contains(m_skill_Frame))
		{
			int index = m_skill_tabcontrol.getPageIndex();
			if (m_skill_bag_grid[index] != null && m_skill_bag_grid[index].length > 0)
			{
				grid = m_skill_bag_grid[index][0];
			}
		}
		return grid;
	}

	/**
	 * 获取技能装备按钮
	 * @return
	 */
	public DWButton getSkillButton()
	{
		DWButton button = null;
		if (m_learn_skill != null && DWControlsManager.getInstance().contains(m_learn_skill))
			button = m_skill_setSkill;

		return button;
	}

	/**
	 * 获取第二个装备技能栏
	 * @return
	 */
	public DWGrid getEquipGrid()
	{
		DWGrid grid = null;

		if (m_skill_Frame != null && DWControlsManager.getInstance().contains(m_skill_Frame))
		{
			grid = m_skill_jineng_grid[1];
		}
		return grid;
	}

	/**
	 * 获取第一个,学习技能按钮
	 * @return
	 */
	public DWButton getShowLearnButton()
	{
		DWButton button = null;

		if (m_skill_Frame != null && DWControlsManager.getInstance().contains(m_skill_Frame))
		{
			int index = m_skill_tabcontrol.getPageIndex();
			if (m_skill_bag_button[index] != null && m_skill_bag_button[index].length > 0)
			{
				button = m_skill_bag_button[index][0];
			}
		}
		return button;
	}

	/**
	 * 获取学习技能界面，学习按钮
	 * @return
	 */
	public DWButton getlearnButton()
	{
		DWButton button = null;

		if (m_learn_skill != null && DWControlsManager.getInstance().contains(m_learn_skill))
		{
			button = learn;
		}
		return button;
	}

	/**
	 * 获取技能界面的关闭按钮
	 * @return
	 */
	public DWImageBox getCloseImg()
	{
		DWImageBox imgbox = null;

		if (m_skill_Frame != null && DWControlsManager.getInstance().contains(m_skill_Frame))
		{
			imgbox = m_skill_close;
		}
		return imgbox;
	}

	/**
	 * 打开重置界面
	 */
	private byte m_reset_viplv=-1;//重置技能的vip等级
	private long m_reset_fee=-1;//重置技能的价格

	public void recvOpenReste(DataInputStream dis) throws IOException
	{
		DWGameManager.getInstance().getSendMessage().unlockNetwork(GameMessage.锁_技能);
		m_reset_fee = dis.readLong();
		m_reset_viplv = dis.readByte();

		showResetFam("您确定要重置所有技能等级？`需要达到vip" + m_reset_viplv + ",花费");
	}

	/**
	 * 重置技能结果
	 */
	public void recvResetResult(DataInputStream dis) throws IOException
	{
		DWGameManager.getInstance().getSendMessage().unlockNetwork(GameMessage.锁_技能);
		byte m_state = dis.readByte();
		String message = "";
		if (m_state == 1)
		{
			message="技能重置成功";
		}
		else
		{
			message="技能重置失败";
		}
		DWGameManager.getInstance().addSystemInfo(DWGameManager.SYSTEM_INFO_TYPE_B, message);
	}

}
